package cj.web.admin.controllers;

import java.util.EnumSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Base64;
import cj.utils.DateUtils;
import cj.web.admin.dao.AppDevicesAndUidDAO;
import cj.web.admin.dao.AppUserInfoDAO;
import cj.web.admin.dao.CommentsDAO;
import cj.web.admin.dao.FeedbackDAO;
import cj.web.admin.domain.AppDevicesAndUid;
import cj.web.admin.domain.AppUserInfo;
import cj.web.admin.domain.Comments;
import cj.web.admin.domain.Feedback;
import cj.web.constants.AppUserInfoStateEnum;
import cj.web.constants.CommentsReleaseStateEnum;
import cj.web.constants.FeedbackEnum;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;

@Path("/appUserInfo")
public class AppUserInfoController extends BaseController{

	private static Logger logger = Logger.getLogger(AppUserInfoController.class.getName());
	
	@Autowired
	private AppUserInfoDAO appUserInfoDao;
	@Autowired
	private AppDevicesAndUidDAO appDevicesAndUidDAO;
	@Autowired
	private FeedbackDAO feedbackDao;
	@Autowired
	private CommentsDAO commentsDao;
	
	@Get("show")
	public String show(Model model,AppUserInfo appUserInfo) {
		//MemuEnum
		model.add("id",appUserInfo.getId());
        model.add("MemuEnum",MemuEnum.lkgzcyhll);
        model.add("AppUserInfoStateEnum",EnumSet.allOf(AppUserInfoStateEnum.class));
		return "/views/news/appUserInfoList.vm";
	}

	@Post("getList")
	public String queryList(AppUserInfo appUserInfo) {
	
		List<AppUserInfo> arr = appUserInfoDao.query(appUserInfo);
		
		for(AppUserInfo obj:arr)
		{
			obj.setName(Base64.getFromBase64(obj.getName()));
			obj.setCreateAt(DateUtils.getMinusTime(obj.getCreateAt(), DateUtils.DATE_FULL_STR, 0));
			obj.setUpdateAt(DateUtils.getMinusTime(obj.getUpdateAt(), DateUtils.DATE_FULL_STR, 0));
		}

		JSONObject jo = new JSONObject();

		int size = appUserInfoDao.querySize(appUserInfo);
		
		jo.put("data", arr);
		jo.put("draw", appUserInfo.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();	
	}
	
	//正常
	@Get("updateStateZC")
    public String updateStateZC(@Param("id") int id) {
		
		AppUserInfo obj = appUserInfoDao.queryById(id);
		
		if(obj!=null)
		{
			appUserInfoDao.updateStateZC(id);
		}
		
		return "r:show";
	}
	
	//禁言
	@Get("updateStateJY")
    public String updateStateJY(@Param("id") int id) {
		
		AppUserInfo obj = appUserInfoDao.queryById(id);
		
		if(obj!=null)
		{
			appUserInfoDao.updateStateJY(id);
		}
		
		return "r:show";
	}
	//-----------------------用户提问列表
	@Get("aufbShow")
	public String appUserFeedBackShow(Model model,Feedback feedback) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgzcyhll);
        model.add("userId",feedback.getUserId());
        model.add("FeedbackEnum",EnumSet.allOf(FeedbackEnum.class));
		return "/views/news/appUserFeedBackList.vm";
	}

	@Post("aufbGetList")
	public String appUserFeedBackQueryList(Feedback feedback,HttpServletRequest request) {
		List<Feedback> arr = feedbackDao.query(feedback);
		
		for(Feedback obj:arr)
		{
			obj.setCreateAt(DateUtils.getMinusTime(obj.getCreateAt(), DateUtils.DATE_FULL_STR, 0));
			
			if(obj.getUpdateAt()!=null){
				obj.setUpdateAt(DateUtils.getMinusTime(obj.getUpdateAt(), DateUtils.DATE_FULL_STR, 0));
			}else{
				obj.setUpdateAt("");
			}
			
		}

		JSONObject jo = new JSONObject();

		int size = feedbackDao.querySize(feedback);

		jo.put("data", arr);
		jo.put("draw", feedback.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	//-----------------------用户评论列表
	@Get("aucShow")
	public String appUserCommentsShow(Model model,Comments comments) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgzcyhll);
        model.add("userId",comments.getUserId());
        model.add("CommentsReleaseStateEnum",EnumSet.allOf(CommentsReleaseStateEnum.class));
		return "/views/news/appUserCommentsList.vm";
	}

	@Post("aucGetList")
	public String appUserCommentsQueryList(Comments comments,HttpServletRequest request) {
		List<Comments> arr = commentsDao.query(comments);
		
		for(Comments obj:arr)
		{
			obj.setCreateAt(DateUtils.getMinusTime(obj.getCreateAt(), DateUtils.DATE_FULL_STR, 0));
			
			if(obj.getUpdateAt()!=null){
				obj.setUpdateAt(DateUtils.getMinusTime(obj.getUpdateAt(), DateUtils.DATE_FULL_STR, 0));
			}else{
				obj.setUpdateAt("");
			}
		}

		JSONObject jo = new JSONObject();

		int size = commentsDao.querySize(comments);
		
		jo.put("data", arr);
		jo.put("draw", comments.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();	
	}
	
	//-----------------------用户评论列表
	@Get("loginShow")
	public String appUserLoginShow(Model model,@Param("devicesId") String devicesId) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgzcyhll);
        model.add("devicesId",devicesId);
		return "/views/news/appUserInfoLoginList.vm";
	}

	@Post("loginGetList")
	public String appUserLoginQueryList(AppDevicesAndUid appDevicesAndUid,HttpServletRequest request) {
		List<AppDevicesAndUid> arr = appDevicesAndUidDAO.query(appDevicesAndUid);
		
		for(AppDevicesAndUid obj:arr)
		{
			obj.setName(Base64.getFromBase64(obj.getName()));
		}
		
		JSONObject jo = new JSONObject();

		int size = appDevicesAndUidDAO.querySize(appDevicesAndUid);
		
		jo.put("data", arr);
		jo.put("draw", appDevicesAndUid.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();	
	}
}
